#!/bin/bash
#


function check_elasticsearch() {
    netstat -tunlp | grep java | grep 9200 &> /dev/null
    REVTAL=$?
    
    if [ $REVTAL -eq 0 ]; then 
        elasticsearch_pid=`netstat -tunlp | grep java | grep 9200 | awk '{print $NF}' | awk -F '/' '{print $1}'`
        echo "$(date +%Y-%m-%d" "%H:%M:%S) - - elasticsearch service is running: ${elasticsearch_pid}."
    else
        su - es -c "/usr/local/elasticsearch/bin/elasticsearch -d"
        echo "elasticsearch service start."
    fi
}


while true; do
    check_elasticsearch
    sleep 120s
done